package com.bh.weapp.controller.customer;

import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.MapUtils;
import com.bh.extend.domain.JcCustomer;
import com.bh.extend.domain.JcCustomerUser;
import com.bh.extend.domain.JcReceivingParty;
import com.bh.extend.service.IJcCustomerService;
import com.bh.extend.service.IJcCustomerUserService;
import com.bh.extend.service.IJcReceivingPartyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户地址Controller
 *
 * @author slx
 * @date 2023-04-02
 */
@Api(tags = {"CUST客户地址API"})
@RestController
@RequestMapping("/api/cust/extend/jcReceivingParty")
public class JcReceivingPartyCustApiController extends BaseController {

    @Autowired
    private IJcReceivingPartyService jcReceivingPartyService;

    @Autowired
    private IJcCustomerUserService jcCustomerUserService;

    @Autowired
    private IJcCustomerService jcCustomerService;

    /**
     * 查询客户地址列表
     */
    @ApiOperation("查询地址分页列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody JcReceivingParty jcReceivingParty) {
        List<JcReceivingParty> list = new ArrayList<>();
        JcCustomerUser customerUser = jcCustomerUserService.selectJcCustomerUserByUserId(getUserId());
        if (customerUser != null) {
            jcReceivingParty.setCustomerId(customerUser.getCustomerId());
            //默认查询自己的
            startPage();
            list = jcReceivingPartyService.selectJcReceivingpartyList(jcReceivingParty);
            return getDataTable(list);
        }
        return getDataTable(list);

    }

    /**
     * 新增保存客户地址 --TODO
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("新增保存客户地址")
    @Log(title = "客户地址", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody JcReceivingParty jcReceivingParty) {
        //默认新增自己的
        JcCustomerUser jcCustomerUser = jcCustomerUserService.selectJcCustomerUserByUserId(getUserId());
        //默认新增自己的
        if (jcCustomerUser != null) {
            jcReceivingParty.setJcZoneId(MapUtils.getAdcode(jcReceivingParty.getLtl()));
            jcReceivingParty.setCustomerId(jcCustomerUser.getCustomerId());

            JcCustomer customer = jcCustomerService.selectJcCustomerById(jcCustomerUser.getCustomerId());
            jcReceivingParty.setName(customer.getName());
            jcReceivingParty.setJcOrganizationId(customer.getJcOrganizationId());
            return toAjax(jcReceivingPartyService.insertJcReceivingparty(jcReceivingParty));
        }
        return error("请先联系销售或管理员绑定所属客户和合同！");
    }

    /**
     * 修改客户地址
     */
    @ApiOperation("查询客户地址")
    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        //默认查询自己的
        JcReceivingParty jcReceivingParty = jcReceivingPartyService.selectJcReceivingpartyById(id);
        return AjaxResult.success(jcReceivingParty);
    }

    /**
     * 修改保存客户地址
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("修改保存客户地址")
    @Log(title = "客户地址", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody JcReceivingParty jcReceivingParty) {
        if (jcReceivingParty.getJcZoneId() == null) {
            jcReceivingParty.setJcZoneId(MapUtils.getAdcode(jcReceivingParty.getLtl()));
        }
        return toAjax(jcReceivingPartyService.updateJcReceivingparty(jcReceivingParty));
    }

    /**
     * 删除客户地址
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("删除客户地址")
    @Log(title = "客户地址", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(Long[] ids) {
        return toAjax(jcReceivingPartyService.deleteJcReceivingpartyByIds(ids));
    }
}
